# -*- coding: utf-8 -*-

import time
import store


def get_rec(uid, num=10):
#    print time.time()
    db = store.ItemsDB('items_index.db')

    def _get_rank(uid_items, item):
        rank = 0
        for i in uid_items:
            r = db.get_item_to_item(i, item)
            if r:
                rank += r
        return rank

    # список подписок
    uid_items = db.get_subscriptions(uid)    
    #print 'subscriptions for uid = %s: %s' % (uid, uid_items)

    #print time.time()

    # список для потенциальных рекомендаций
    _items = db.get_shadow_items(uid_items)
    #print 'recomendations from %s items for uid = %s' % (len(_items), uid)

    #print time.time()

    #_items = []
    # список пар item-rank
    ranked_items = []
    for i in _items:
        r = _get_rank(uid_items, i)
        ranked_items.append((r, i))
    
    #print time.time()

    return sorted(ranked_items, reverse=True)[:num]

def get_rec2(uid, num=10):
#    print time.time()
    db = store.ItemsDB('items_index.db')

    # список подписок
    uid_items = db.get_subscriptions(uid)    
    #print 'subscriptions for uid = %s: %s' % (uid, uid_items)

    #print time.time()

    # список пар item-rank
    ranked_items = db.get_shadow_items_sort(uid_items, num)

    #print time.time()

    return sorted(ranked_items, reverse=True)[:num]


def print_ranks(ranks):
    print ''
    print '-------------------------'
    print '   items   :   ranks '
    print '-------------------------'
    
    for (r, i) in ranks:
        print '%10i : %10.8f' % (i, r)
    
    print ''
    
    items_str = ', '.join(map(str, [i for (r, i) in ranks]))
    print 'items: %s' % items_str


uid1 = 42142368
uid2 = 37769329

